class EventsController < ApplicationController
  # GET /events
  # GET /events.xml
  def index   
    # full_calendar will hit the index method with query parameters
    # 'start' and 'end' in order to filter the results for the
    # appropriate month/week/day.  It should be possiblt to change
    # this to be starts_at and ends_at to match rails conventions.
    # I'll eventually do that to make the demo a little cleaner.

    @events = Event.find_all_by_resource_id(params['resourceid'])
    @user_timezone = current_user.project.time_zone
    
	respond_to do |format|
	format.html { render :partial => "events/eventsresource.json" }
    end
  end

  def getresource
		#@input_id = params[:resourceid]
		@resourceOut = current_user.permission.resources;
		
    respond_to do |format|
      format.html { render :partial => "events/getresource.json" }
    end		
  end
  
  # GET /events/1
  # GET /events/1.xml
  def show
    #@event = Event.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
      format.js { render :json => @event.to_json }
    end
  end

  def getworkorderinfo
    @event = Event.find(params[:id])
    @user_timezone = params[:timezone]
    
    puts "#######################"
    puts @event.work_order_date.in_time_zone(@user_timezone).strftime("%Y-%m-%d %H:%M")
    
    respond_to do |format|
      format.html { render :partial => "events/getworkorderinfo.json" }
    end
  end  
  
  # GET /events/new
  # GET /events/new.xml
  def new
    
    @workordertype_data = Workordertype.find(:all) #put Workorder to query data in new.html.erb
    @event = Event.new
    respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @event }
    end
  end

  def newworkordertype
     @workordertype = Workordertype.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @workordertype }
    end 
  end

  def newresource
     @resourcegroup_data = Resourcegroup.find(:all)
     @resource = Resource.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @resource }
    end 
  end  

  def newresourcegroup
 
     @resourcegroup = Resourcegroup.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @resource }
    end 
  end  
  
  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
  end

  # POST /events
  # POST /events.xml
  def create
    user_timezone = current_user.project.time_zone
    
    
    if params[:event][:work_order_date]
        params[:event][:work_order_date] = ActiveSupport::TimeZone[user_timezone].parse(params[:event][:work_order_date].to_s).utc.strftime('%Y-%m-%d %H:%M').to_s
        #puts "THIS IS:   CONVERTED TO UTC" + params[:event][:work_order_date].to_s
    end

    if params[:event][:due_date]
        params[:event][:due_date] = ActiveSupport::TimeZone[user_timezone].parse(params[:event][:due_date].to_s).utc.strftime('%Y-%m-%d %H:%M').to_s
        #puts "THIS IS:   CONVERTED TO UTC" + params[:event][:due_date].to_s
    end 
    
    @event = Event.new(params[:event])
    
    respond_to do |format|
      if @event.save
	flash[:notice] = "Workorder successfully created"
        format.html { redirect_to :controller => "events" , :action => "new" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
	flash[:notice] = "Workorder cannot be created"
        format.html { redirect_to :controller => "events" , :action => "new" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  def createworkordertype
    #puts 'HAHAHAHAHAHAHAHHA =' + params[:workordertype].type.to_s;
	@workordertype = Workordertype.new(params[:workordertype])
	
    respond_to do |format|
      if @workordertype.save
	flash[:notice] = "Workorder type successfully created"
        format.html { render :controller => "events" , :action => "newworkordertype" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
		flash[:notice] = "Workorder type cannot be created"
        format.html { render :controller => "events" , :action => "newworkordertype" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end
  
    def createresource
    #puts 'HAHAHAHAHAHAHAHHA =' + params[:workordertype].type.to_s;
	@resource = Resource.new(params[:resource])
	
    respond_to do |format|
      if @resource.save
	flash[:notice] = "Resource successfully created"
        format.html { redirect_to :controller => "events" , :action => "newresource" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
	flash[:notice] = "Resource cannot be created"
        format.html { redirect_to :controller => "events" , :action => "newresource" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

    def createresourcegroup
    #puts 'HAHAHAHAHAHAHAHHA =' + params[:workordertype].type.to_s;
	@resourcegroup = Resourcegroup.new(params[:resourcegroup])
	
    respond_to do |format|
      if @resourcegroup.save
	flash[:notice] = "Resource successfully created"
        format.html { render :controller => "events" , :action => "newresourcegroup" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
	flash[:notice] = "Resource group cannot be created"
        format.html { render :controller => "events" , :action => "newresourcegroup" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end  

  def updateDragEvent
    @event = Event.find(params[:id])
    #puts "THIS IS:   ARRAYYYYYYYYYYYYYYYYYYYYYYYYYYYY" + params[:event][:starts_at].to_s
    respond_to do |format|
      if @event.save
	flash[:notice] = "Workorder successfully created"
        format.html { render :controller => "hello" , :action => "index" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
	flash[:notice] = "Workorder cannot be created"
        #format.html { render :action => "new" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  # PUT /events/1.js
  # when we drag an event on the calendar (from day to day on the month view, or stretching
  # it on the week or day view), this method will be called to update the values.
  # viv la REST!
  def update
    #puts "TESTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"
    #puts params[:timezone]
    if params[:event][:starts_at]
        #puts "IN:   ARRAYYYYYYYYYYYYYYYYYYYYYYYYYYYY" + params[:event][:starts_at].to_s 
        params[:event][:starts_at] = ActiveSupport::TimeZone[params[:timezone]].parse(params[:event][:starts_at].to_s).utc.strftime('%Y-%m-%d %H:%M').to_s
        #puts "OUT:   ARRAYYYYYYYYYYYYYYYYYYYYYYYYYYYY" + params[:event][:starts_at].to_s
        params[:event][:ends_at] = ActiveSupport::TimeZone[params[:timezone]].parse(params[:event][:ends_at].to_s).utc.strftime('%Y-%m-%d %H:%M').to_s
    end
    
    
    if params[:event][:resource_id].is_a? (Array)
	 #puts "THIS IS:   ARRAYYYYYYYYYYYYYYYYYYYYYYYYYYYY" + params[:event][:resource_id].to_s
	 params[:event][:resource_id] = params[:event][:resource_id][0]
	end
   
    @event = Event.find(params[:id])
    
    respond_to do |format|
      if @event.update_attributes(params[:event])
        format.html { redirect_to(@event, :notice => 'Event was successfully updated.') }
        #format.xml  { head :ok }
        #format.js { head :ok}
      else
        format.html { render :action => "edit" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
        #format.js  { render :js => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  def unassignevent
	
	@event = Event.find(params[:id])
	@event.starts_at = nil
	@event.ends_at = nil
	@event.resource_id = nil
	@event.save
  end


  def destroy
    @event = Event.find(params[:id])
	@event.destroy
	@event.save

    #respond_to do |format|
      #format.html { redirect_to(events_url) }
      #format.xml  { head :ok }
    #end
  end
end
